// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <string>
#include <pollux/dwio/common/options.h>
#include <pollux/python/type/PyType.h>

namespace kumo::pollux::py {

class PyFile {
 public:
  PyFile(const std::string& filePath, dwio::common::FileFormat fileFormat)
      : filePath_(filePath), fileFormat_(fileFormat) {}

  PyFile(std::string filePath, std::string formatString);

  std::string toString() const;

  std::string filePath() const {
    return filePath_;
  }

  dwio::common::FileFormat fileFormat() const {
    return fileFormat_;
  }

  /// Returns the schema (RowType) in the given file. This function will open
  /// and read metadata from the file using the corresponding reader.
  PyType getSchema();

  static PyFile createParquet(const std::string& filePath) {
    return PyFile(filePath, dwio::common::FileFormat::PARQUET);
  }

  static PyFile createDwrf(const std::string& filePath) {
    return PyFile(filePath, dwio::common::FileFormat::DWRF);
  }

  static PyFile createNimble(const std::string& filePath) {
    return PyFile(filePath, dwio::common::FileFormat::NIMBLE);
  }

  static PyFile createOrc(const std::string& filePath) {
    return PyFile(filePath, dwio::common::FileFormat::ORC);
  }

  static PyFile createJson(const std::string& filePath) {
    return PyFile(filePath, dwio::common::FileFormat::JSON);
  }

  static PyFile createText(const std::string& filePath) {
    return PyFile(filePath, dwio::common::FileFormat::TEXT);
  }

  bool equals(const PyFile& other) const {
    return filePath_ == other.filePath_ && fileFormat_ == other.fileFormat_;
  }

 private:
  const std::string filePath_;
  const dwio::common::FileFormat fileFormat_;

  // Stores the file schema. Lazily instantiated by getSchema(), as it requires
  // the file to be opened and read.
  TypePtr fileSchema_;
};

} // namespace kumo::pollux::py
